const Router = require('@koa/router')

const router = new Router()

const { query } = require('../../utils/async-db')
async function selectAllData( ) {
  let sql = 'SELECT * FROM users' // 'SELECT * FROM user where id=?', [1]
  // let sql = 'SELECT * FROM users WHERE `id`=?'
  let dataList = await query( sql )
  return dataList
}

// async function getData() {
//   let dataList = await selectAllData()
//   console.log( dataList )
// }

// getData()

router.get('/list', async (ctx, next) => {
  // ctx.body = ['T-Shirt', 'Shoes']
  let dataList = await selectAllData()
  await ctx.render('succ.ejs', {
    data: JSON.stringify(dataList)
  }) 
})

router.post('/add', async (ctx, next) => {
  const data = ctx.request.body
  // ctx.body = data
  // await ctx.render('succ', {
  //   data: JSON.stringify(data)
  // })

  await ctx.render('fail.html', {
    message: JSON.stringify('fail')
  })
})

module.exports = router
